# -*- coding: utf-8 -*-
from django import forms
from django.contrib.auth.models import User

class RegisterForm(forms.Form):
    username = forms.CharField(max_length=64)
    password = forms.CharField(max_length=64)
    repeat_password = forms.CharField(max_length=64)
    email = forms.EmailField()

    def clean_username(self):
        username = self.cleaned_data.get("username")
        if User.objects.filter(username=username).count():
            raise forms.ValidationError("Пользователь с таким логином уже зарегистрирован")
        return username


    def clean_email(self):
        email = self.cleaned_data.get("email")
        if User.objects.filter(email=email).count():
            raise forms.ValidationError("Пользователь с таким e-mail уже зарегистрирован")
        return email


    def clean_repeat_password(self):
        data = self.cleaned_data
        if not data.get("password") == data.get("repeat_password"):
            raise forms.ValidationError("Пароли должны совпадать")
        return data.get("repeat_password")